from django.shortcuts import render,redirect,HttpResponse
from stuinfo.models import student
from django.core.paginator import Paginator
# Create your views here.

#获取表单内容
def stu_huo(request):
    if request.method == "GET":
        return render(request, 'add.html')
    if request.method == "POST":
        stu_name = request.POST.get("stu_name")
        sex = request.POST.get("sex")
        stu_class = request.POST.get("stu_class")
        specialized = request.POST.get("specialized")
        #获取头像
        file = request.FILES.get("file")
        with open('{}','wb') as f:
            for chunk in file.chunks():
                f.write(chunk)
        return HttpResponse("姓名:{},性别:{},年级:{},专业:{},头像:{}".format(stu_name, sex, stu_class, specialized, file))

#展示
def stulist(request):
    #获取所有数据
    list=student.objects.all()
    #每页分几条数据
    pahinator=Paginator(list,3)
    x=request.GET.get("page",1)
    pagex=pahinator.get_page(x)
    return render(request,'index.html',locals())

#删除
def del_stu(request,stuid):
    try:
        studel=student.objects.get(id=stuid)
        studel.delete()
    except:
        return HttpResponse({'error': "错误了"})
    #重定向到展示页
    return redirect('stulist')

#查看
def get_stu(request,stuid):
    stuget=student.objects.get(id=stuid)
    return render(request,'stu_info.html',locals())